﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using FI.Core.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace FI.Core
{
    /// <summary>
    /// The message fault for validation.
    /// </summary>
    [DataContract(Namespace = Constants.Namespace)]
    public class ValidationFault
    {
        /// <summary>
        /// Gets the <see cref="ValidationError"/> list.
        /// </summary>
        /// <value>
        /// The <see cref="ValidationError"/> list.
        /// </value>
        [DataMember]
        public IList<ValidationError> Errors { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationFault"/> class.
        /// </summary>
        public ValidationFault()
            : this(new ValidationError[0])
        {}

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationFault"/> class.
        /// </summary>
        /// <param name="errors">The errors.</param>
        public ValidationFault(IEnumerable<ValidationError> errors)
        {
            Guard.ArgumentNotNull(errors, "errors");
            this.Errors = errors.ToList();
        }

        /// <summary>
        /// Adds the specified error.
        /// </summary>
        /// <param name="error">The error.</param>
        public void Add(ValidationError error)
        {
            Guard.ArgumentNotNull(error, "error");
            this.Errors.Add(error);
        }

        /// <summary>
        /// Gets a value indicating whether this instance is valid.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </value>
        public bool IsValid
        {
            get { return !this.Errors.Any();}
        }

        /// <summary>
        /// Gets the fault action.
        /// </summary>
        /// <param name="contractNamespace">The contract namespace.</param>
        /// <param name="operationName">Name of the operation.</param>
        /// <returns>The fault action name.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]
        public static string GetFaultAction(string contractNamespace, string operationName)
        {
            contractNamespace = contractNamespace ?? "";
            return string.Format("{0}/{1}_ValidationFault", contractNamespace.TrimEnd("/".ToCharArray()), operationName);
        }
    }
}
